﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Pharmacy_management_system
{
    public partial class frmLock : Form
    {
        public frmLock()
        {
            InitializeComponent();
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                this.button1_Click(sender, e);
            }
        }
        private void frmLock_Load(object sender, EventArgs e)
        {
            try
            {
                using (SqlConnection con = new SqlConnection(SQL.constr))
                {
                    SqlDataAdapter da = new SqlDataAdapter("select name from dict_employee where py_code='"+DangQianHang.cc+"'", con);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    string name = dt.Rows[0][0].ToString();
                    this.label3.Text = "【" + name + "】";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            this.textBox1.Text = DangQianHang.cc;
            this.textBox2.Focus();            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int i = 0;
            try
            {
                using (SqlConnection conn = new SqlConnection(SQL.constr))
                {

                    conn.Open();
                    //string abc = "select count(*) from sys_sys_func where user_name='" + textBox1.Text.Trim() + "' and pass_word='" + textBox2.Text.Trim() + "'";
                    //SqlCommand comm = new SqlCommand("select count(*) from sys_sys_func where user_name='" + textBox1.Text.Trim() + "' and pass_word='" + textBox2.Text.Trim() + "'", conn);
                    
                    SqlCommand comm = conn.CreateCommand();
                    comm.CommandText = "select count(*) from sys_sys_users where user_name=@user_name and pass_word=@pass_word  ";

                    SqlParameter p1 = new SqlParameter("@user_name", SqlDbType.NVarChar);
                    p1.Value = this.textBox1.Text;
                    comm.Parameters.Add(p1);

                    SqlParameter p2 = new SqlParameter("@pass_word", SqlDbType.NVarChar);
                    p2.Value = this.textBox2.Text;
                    comm.Parameters.Add(p2);

                    i = (int)comm.ExecuteScalar();

                    if (i == 1)
                    {
                        hide = true;
                        hide_Main(hide);
                        notifyIcon1.Visible = false;
                        this.Close();
                    }
                    else
                    {
                        this.label5.Visible = true;
                        this.timer1.Interval = 2000;
                        this.timer1.Start();
                    }
                }
            }


            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }


           

        }

        private void frmLock_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                if (hide == false)
                {
                    e.Cancel = true;
                    notifyIcon1.Visible = true;
                    this.Hide();
                    this.notifyIcon1.ShowBalloonTip(100, "提示：", "临床药学管理系统已最小化至托盘。", ToolTipIcon.Info);
                }
                else
                {
                    e.Cancel = false;
                    notifyIcon1.Visible = false;
                    this.Hide();
                }

            }

        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            //鼠标双击任务栏图标显示程序界面
            if (this.Visible == false)
            {
                this.Visible = true;
                notifyIcon1.Visible = false;
            }

        }
        public delegate void traseferdate(bool hide);
        public event traseferdate hide_Main;
        bool hide = false;

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.label8.Visible = false;

        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.button1_Click(sender, e);
            }

        }

    }
}
